home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Pascal / Snippets / PNL Libraries / Libraries / SpriteWorld / SpriteWorld files / Utils Pascal / SWGameUtils.p < prev   
Text File  |  1997-04-16  |  9KB  |  291 lines

  1. { /-------------------------------------------------------------------------------------- }
  2. {     SWGameUtils.c }
  3. {  }
  4. {     Portions are copyright: © 1991-94 Tony Myles, All rights reserved worldwide. }
  5. {  }
  6. {     Description:    some utility functions for games }
  7. { /-------------------------------------------------------------------------------------- }
  8.  
  9. unit SWGameUtils;
  10.  
  11. interface
  12.  
  13.     uses
  14. {$IFC undefined THINK_Pascal}
  15.         Types, Quickdraw, 
  16. {$ENDC}
  17.         QDOffscreen, SWCommonHeaders;
  18.  
  19.  
  20.     procedure InitGameUtils;
  21.  
  22.     procedure Randomize;
  23.     function GetRandom (min: integer; max: integer): integer;
  24.     procedure CenterRect (var rectA: Rect; var rectB: Rect);
  25.     procedure AllowKeyUpEvents;
  26.     procedure RestoreEventMask;
  27.     function HideMenuBar (grafPort: GrafPtr): RgnHandle;
  28.     procedure ShowMenuBar (grafPort: GrafPtr);
  29.     function GetDepthFromWindow (theWindow: WindowPtr): integer;
  30.     function GetScreenDepth (theGDH: GDHandle): integer;
  31.  
  32. implementation
  33.  
  34. {$IFC undefined THINK_Pascal}
  35.     uses
  36.         OSUtils, Menus, LowMem, Events, GestaltEqu;
  37. {$ENDC}
  38.  
  39. { /-------------------------------------------------------------------------------------- }
  40. {     Randomize - initialize random number seed }
  41. { /-------------------------------------------------------------------------------------- }
  42.  
  43.     procedure Randomize;
  44.     begin
  45. {$IFC undefined THINK_Pascal}
  46.         GetDateTime(UInt32(qd.randSeed));
  47. {$ELSEC}
  48.         GetDateTime(randSeed);
  49. {$ENDC}
  50.     end;
  51.  
  52.  
  53. { /-------------------------------------------------------------------------------------- }
  54. {     GetRandom - generate a random number between min and max inclusive }
  55. { /-------------------------------------------------------------------------------------- }
  56.  
  57.     function GetRandom (min: integer; max: integer): integer;
  58.         var
  59.             value: integer;
  60.             range, temp: longint;
  61.     begin
  62.         value := Random;
  63.         range := (max - min) + 1;
  64.         temp := (value * range) div 65536;
  65.         value := temp + min;
  66.  
  67.         GetRandom := value;
  68.     end;
  69.  
  70.  
  71. { /-------------------------------------------------------------------------------------- }
  72. {     CenterRect - centers rectA in rectB }
  73. { /-------------------------------------------------------------------------------------- }
  74.  
  75.     procedure CenterRect (var rectA: Rect; var rectB: Rect);
  76.         var
  77.             width, height: integer;
  78.  
  79.     begin
  80.         width := (rectA.right - rectA.left);
  81.         height := (rectA.bottom - rectA.top);
  82.  
  83.         rectA.left := rectB.left + (((rectB.right - rectB.left) div 2) - (width div 2));
  84.         rectA.top := rectB.top + (((rectB.bottom - rectB.top) div 2) - (height div 2));
  85.         rectA.right := rectA.left + width;
  86.         rectA.bottom := rectA.top + height;
  87.     end;
  88.  
  89.     var
  90.  
  91.         gOldEventMask: integer;    {  Used by AllowKeyUpEvents and RestoreEventMask }
  92.         eventMaskIsGood: Boolean;
  93.  
  94. { /-------------------------------------------------------------------------------------- }
  95. {     AllowKeyUpEvents - allows keyUpEvents. Make sure to call RestoreEventMask }
  96. {     before your program quits if you call AllowKeyUpEvents. }
  97. { /-------------------------------------------------------------------------------------- }
  98.  
  99.     procedure AllowKeyUpEvents;
  100.     begin
  101.         gOldEventMask := LMGetSysEvtMask;
  102.         SetEventMask(everyEvent);
  103.         {  Let RestoreEventMask know that the old mask has been saved }
  104.         eventMaskIsGood := true;
  105.     end;
  106.  
  107.  
  108. { /-------------------------------------------------------------------------------------- }
  109. {     RestoreEventMask - call this before your program quits if you previously }
  110. {     called AllowKeyUpEvents. This will beep if you never called AllowKeyUpEvents first. }
  111. { /-------------------------------------------------------------------------------------- }
  112.  
  113.     procedure RestoreEventMask;
  114.     begin
  115.         if (eventMaskIsGood) then
  116.             SetEventMask(gOldEventMask)
  117.         else
  118.             SysBeep(1);
  119.     end;
  120.  
  121.  
  122.  
  123. { /-------------------------------------------------------------------------------------- }
  124. {     Globals for HideMenuBar and ShowMenuBar }
  125. { /-------------------------------------------------------------------------------------- }
  126.  
  127.     var
  128.  
  129.         gOldVisRgn: RgnHandle;    {  visRgn of window before hiding menu bar }
  130.         gUpdateRgn: RgnHandle;    {  region returned to user }
  131.         gOldMBarHeight: integer;
  132.  
  133.  
  134. { /-------------------------------------------------------------------------------------- }
  135. {     HideMenuBar - expands the vis region of grafPort to cover the entire window, which }
  136. {  will allow you to draw in the top of that window to erase the menu bar. This is a }
  137. {  simple routine designed for programs with only one window that covers the menu bar. }
  138. {  If you need to expand the region of more than one window, you need a different routine. }
  139. {  Be sure to make the window visible before calling this. HideMenuBar returns the }
  140. {  region of the menu bar and corners of the screen, in case you want to erase or }
  141. {  draw in that area. }
  142. { /-------------------------------------------------------------------------------------- }
  143.  
  144.     procedure InitGameUtils;
  145.     begin
  146.         gOldVisRgn := nil;
  147.         gUpdateRgn := nil;
  148.         eventMaskIsGood := False;
  149.     end;
  150.  
  151.     function HideMenuBar (grafPort: GrafPtr): RgnHandle;
  152.         var
  153.             newVisRgn: RgnHandle;
  154.             savePort: GrafPtr;
  155.     begin
  156.         if (gOldVisRgn <> nil) then
  157.             begin
  158.                 HideMenuBar := nil;
  159.                 exit(HideMenuBar);
  160.             end;
  161.  
  162.         GetPort(savePort);
  163.         SetPort(grafPort);
  164.  
  165.         gOldMBarHeight := LMGetMBarHeight;
  166.         LMSetMBarHeight(0);        {  Keeps things like SuperClock from coming on. }
  167.  
  168.         {  save off vis region }
  169.         gOldVisRgn := NewRgn;
  170.         CopyRgn(grafPort^.visRgn, gOldVisRgn);
  171.  
  172.         {  expand the vis region of the port rect to be completely rectangular }
  173.         newVisRgn := NewRgn;
  174.         RectRgn(newVisRgn, grafPort^.portRect);
  175.         CopyRgn(newVisRgn, grafPort^.visRgn);
  176.         DisposeRgn(newVisRgn);
  177.  
  178.         {  set gUpdateRgn to region of rounder corners and menu bar }
  179.         gUpdateRgn := NewRgn;
  180.         CopyRgn(gOldVisRgn, gUpdateRgn);
  181.         DiffRgn(grafPort^.visRgn, gUpdateRgn, gUpdateRgn);
  182.  
  183.         SetPort(savePort);
  184.         HideMenuBar := gUpdateRgn;
  185.     end;
  186.  
  187. { /-------------------------------------------------------------------------------------- }
  188. {     ShowMenuBar - restores the grafPort to the way it was before the call to HideMenuBar. }
  189. {     Make sure to call this after every call to HideMenuBar to dispose of gOldVisRgn. }
  190. { /-------------------------------------------------------------------------------------- }
  191.  
  192.     procedure ShowMenuBar (grafPort: GrafPtr);
  193.         var
  194.             savePort: GrafPtr;
  195.             junkRgn: RgnHandle;
  196.  
  197.     begin
  198.  
  199.         if (gOldVisRgn = nil) then
  200.             exit(ShowMenuBar);
  201.  
  202.         GetPort(savePort);
  203.         SetPort(grafPort);
  204.  
  205.         LMSetMBarHeight(gOldMBarHeight);
  206.  
  207.         {  fill the rounded corners of the screen with black again }
  208.         junkRgn := NewRgn;
  209.         CopyRgn(gOldVisRgn, junkRgn);
  210.         DiffRgn(grafPort^.visRgn, junkRgn, junkRgn);
  211.  
  212. {$IFC undefined THINK_Pascal}
  213.         FillRgn(junkRgn, qd.black);
  214. {$ELSEC}
  215.         FillRgn(junkRgn, black);
  216. {$ENDC}
  217.  
  218.         DisposeRgn(junkRgn);
  219.  
  220.         {  restore the old vis region }
  221.         CopyRgn(gOldVisRgn, grafPort^.visRgn);
  222.         DisposeRgn(gOldVisRgn);
  223.         gOldVisRgn := nil;
  224.  
  225.         DisposeRgn(gUpdateRgn);
  226.         gUpdateRgn := nil;
  227.  
  228.         DrawMenuBar;
  229.  
  230.         SetPort(savePort);
  231.     end;
  232.  
  233.  
  234. { /-------------------------------------------------------------------------------------- }
  235. {     GetDepthFromWindow }
  236. { /-------------------------------------------------------------------------------------- }
  237.  
  238.     function GetDepthFromWindow (theWindow: WindowPtr): integer;
  239.         var
  240.             oldGWorld, windowGWld: GWorldPtr;
  241.             oldGDH, windowGDH: GDHandle;
  242.             depth: integer;
  243.  
  244.     begin
  245.         GetGWorld(oldGWorld, oldGDH);
  246.         SetPort(theWindow);
  247.         GetGWorld(windowGWld, windowGDH);
  248.  
  249.         depth := GetScreenDepth(windowGDH);
  250.  
  251.         SetGWorld(oldGWorld, oldGDH);
  252.         GetDepthFromWindow := depth;
  253.     end;
  254.  
  255. { /-------------------------------------------------------------------------------------- }
  256. {     GetScreenDepth }
  257. { /-------------------------------------------------------------------------------------- }
  258.  
  259.     function GetScreenDepth (theGDH: GDHandle): integer;
  260.         var
  261.             theSERec: SysEnvRec;
  262.             thePixMap: PixMapHandle;
  263.             err: OSErr;
  264.     begin
  265.  
  266.         err := SysEnvirons(2, theSERec);
  267.         if (not theSERec.hasColorQD) then                {  no colorQD? }
  268.             GetScreenDepth := 1
  269.         else
  270.             begin
  271.                 thePixMap := theGDH^^.gdPMap;
  272.                 GetScreenDepth := thePixMap^^.pixelSize;     {  Depth in bits (1,2,4...) }
  273.             end;
  274.     end;
  275.  
  276.  
  277. { /-------------------------------------------------------------------------------------- }
  278. {     HasSystem7 }
  279. { /-------------------------------------------------------------------------------------- }
  280.  
  281.     function HasSystem7: Boolean;
  282.         var
  283.             gestaltResponse: longint;
  284.             err: OSErr;
  285.     begin
  286.         err := Gestalt(gestaltSystemVersion, gestaltResponse);
  287.         HasSystem7 := (err = noErr) and (gestaltResponse >= $0700);
  288.     end;
  289.  
  290. end.
  291.